package com.zerowidth.moc.web.controller;


import com.zerowidth.db.bean.TeachBean;
import com.zerowidth.db.bean.vm.VipInfoVM;
import com.zerowidth.moc.web.auth.annotation.NeedToken;
import com.zerowidth.moc.web.auth.annotation.NoToken;
import com.zerowidth.moc.web.bean.YsePayBean;
import com.zerowidth.moc.web.controller.service.PayService;
import com.zerowidth.moc.web.resp.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.List;

@RestController
@RequestMapping("/api/v1")
public class OrderController {

    @Autowired
    PayService payService;

    /**
     * 获取购买会员的信息，价格、会员权益说明
     */
    @NoToken
    @GetMapping("/vipList")
    public ResponseEntity<ResultBean<VipInfoVM>> getVipEnumList(
            HttpServletRequest request,
            @RequestParam(value = "onlyUnbind", required = false) boolean onlyUnbind,
            @RequestParam(value = "dingSafe", required = false) boolean dingSafe) {

        String appName = URLDecoder.decode(request.getHeader("appName"));
        String app_channel = request.getHeader("channel");

        return payService.getVipEnumList(appName, app_channel, onlyUnbind, dingSafe);
    }


    /**
     * 以旧换新
     */
    @NeedToken
    @PostMapping("/vip/exchange")
    public ResponseEntity<ResultBean<Void>> exchange(
            HttpServletRequest request,
            @RequestParam("old_stat_id") String old_stat_id,
            @RequestParam("new_stat_id") String new_stat_id) {

        return payService.exchange(request, old_stat_id, new_stat_id);
    }

    //////////////////////////////////////////   微信支付      //////////////////////////////////////

    /**
     * 微信APP支付创建购买宝宝定位会员订单
     */
    @NoToken
    @PostMapping("/order/wx")
    public ResponseEntity<ResultBean<String>> createWXAppOrder(
            HttpServletRequest request,
            @RequestParam("vip_type") int vipType,
            @RequestParam(value = "unbind_user_id", required = false) String unbind_user_id) {
        return payService.createWXOrder(request, vipType, unbind_user_id);
    }

    /**
     * 微信扫码支付创建购买宝宝定位会员订单
     */
    @NoToken
    @PostMapping("/order/wx/qrcode")
    public ResponseEntity<ResultBean<String>> createWXQrOrder(
            HttpServletRequest request,
            @RequestParam("vip_type") int vipType,
            @RequestParam(value = "unbind_user_id", required = false) String unbind_user_id) {
        return payService.createWXQrOrder(request, vipType, unbind_user_id);
    }


    /**
     * 微信支付成功后的异步通知，由微信支付的服务器发起
     */
    @NoToken
    @PostMapping("/pay_notify/wx")
    public ResponseEntity<ResultBean<Void>> wxPayNotifyFromWxServer(HttpServletRequest request) {
        return payService.wxPayNotifyFromWxServer(request);
    }

    //////////////////////////////////////////   支付宝支付      //////////////////////////////////////

    /**
     * 支付宝创建宝宝定位会员订单
     */
    @NoToken
    @PostMapping("/order/ali")
    public ResponseEntity<ResultBean<String>> createAlipayOrder(
            HttpServletRequest request,
            @RequestParam("vip_type") int vipType,
            @RequestParam(value = "unbind_user_id", required = false) String unbind_user_id) {
        return payService.createAlipayOrder(request, vipType, unbind_user_id);
    }

    /**
     * 支付宝服务商在支付成功后的异步通知
     */
    @NoToken
    @PostMapping("/pay_notify/ali")
    public ResponseEntity<ResultBean<Void>> alipayNotifyFromAliServer(HttpServletRequest request) {
        return payService.alipayNotifyFromAliServer(request);
    }

    //////////////////////////////////////////   汇联支付      //////////////////////////////////////

    /**
     * 汇联支付服务商在支付成功后的异步通知
     */
    @NoToken
    @PostMapping("/pay_notify/hl")
    public ResponseEntity<String> hlPayNotifyFromAliServer(HttpServletRequest request) {
        return payService.hlPayNotifyFromAliServer(request);
    }

    //////////////////////////////////////////   蓝兔支付      //////////////////////////////////////


    /**
     * 蓝兔支付成功后的异步通知，由蓝免支付的服务器发起
     */
    @NoToken
    @PostMapping("/pay_notify/lt")
    public ResponseEntity<String> ltPayNotify(HttpServletRequest request) {
        return payService.ltPayNotify(request);
    }

    //////////////////////////////////////////   联速支付      //////////////////////////////////////


    /**
     * 联速支付成功后的异步通知，由联速支付的服务器发起
     */
    @NoToken
    @PostMapping("/pay_notify/ls")
    public ResponseEntity<String> lsPayNotifyFromWxServer(HttpServletRequest request) {
        return payService.lsPayNotifyFromWxServer(request);
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////


    /**
     * 银盛扫码支付创建购买宝宝定位会员订单
     */
    @NoToken
    @PostMapping("/order/ysepay/qrcode")
    public ResponseEntity<ResultBean<YsePayBean>> createYSEQrOrder(
            HttpServletRequest request,
            @RequestParam("vip_type") int vipType,
            @RequestParam("unbind_user_id") String unbind_user_id) {
        return payService.createYSEQrOrder(request, vipType, unbind_user_id);
    }

    /**
     * 安卓客户端检测到支付成功后的系统通知，由安卓手机发起
     *
     * @param channel 支付渠道，例值：ysepay
     * @param payType 支付方式，1=微信支付，2=支付宝
     * @param money   金额，单位=元，例值："1.20"
     */
    @NoToken
    @PostMapping("/pay_notify/android")
    public ResponseEntity<ResultBean<String>> androidPayNotifyFromPhone(
            HttpServletRequest request,
            @RequestParam("channel") String channel,
            @RequestParam("pay_type") int payType,
            @RequestParam("money") String money) {
        return payService.androidPayNotifyFromPhone(request, channel, payType, money);
    }


}